package cn.powerone.scm.service.impl;

import java.math.BigDecimal;
import java.util.List;
import cn.powerone.common.utils.DateUtils;
import cn.powerone.scm.domain.ScmCustomerOrders;
import cn.powerone.scm.service.IScmCustomerOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.powerone.scm.mapper.ScmOrderReceivablesMapper;
import cn.powerone.scm.domain.ScmOrderReceivables;
import cn.powerone.scm.service.IScmOrderReceivablesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 回款Service业务层处理
 * 
 * @author PowerOne
 * @date 2025-08-05
 */
@Service
public class ScmOrderReceivablesServiceImpl implements IScmOrderReceivablesService 
{
    @Autowired
    private ScmOrderReceivablesMapper scmOrderReceivablesMapper;
    @Autowired
    private IScmCustomerOrdersService scmCustomerOrdersService;

    /**
     * 查询回款
     * 
     * @param id 回款主键
     * @return 回款
     */
    @Override
    public ScmOrderReceivables selectScmOrderReceivablesById(Long id)
    {
        return scmOrderReceivablesMapper.selectScmOrderReceivablesById(id);
    }

    /**
     * 查询回款列表
     * 
     * @param scmOrderReceivables 回款
     * @return 回款
     */
    @Override
    public List<ScmOrderReceivables> selectScmOrderReceivablesList(ScmOrderReceivables scmOrderReceivables)
    {
        return scmOrderReceivablesMapper.selectScmOrderReceivablesList(scmOrderReceivables);
    }

    /**
     * 新增回款
     * 
     * @param scmOrderReceivables 回款
     * @return 结果
     */
    @Override
    @Transactional
    public int insertScmOrderReceivables(ScmOrderReceivables scmOrderReceivables)
    {
        ScmCustomerOrders scmCustomerOrders =scmCustomerOrdersService.selectScmCustomerOrdersById(scmOrderReceivables.getOrderId());
        if (scmCustomerOrders == null){
            throw new RuntimeException("订单编号非法");
        }
        BigDecimal newTotalAmount = scmCustomerOrders.getReceivedAmount().add(scmOrderReceivables.getMoney());
        scmCustomerOrders.setReceivedAmount(newTotalAmount);
        scmCustomerOrdersService.updateScmCustomerOrders(scmCustomerOrders);

        scmOrderReceivables.setCustomerId(scmCustomerOrders.getCustomerId());
        scmOrderReceivables.setCreateTime(DateUtils.getNowDate());
        return scmOrderReceivablesMapper.insertScmOrderReceivables(scmOrderReceivables);
    }

    /**
     * 修改回款
     * 
     * @param scmOrderReceivables 回款
     * @return 结果
     */
    @Override
    @Transactional
    public int updateScmOrderReceivables(ScmOrderReceivables scmOrderReceivables)
    {
        ScmCustomerOrders scmCustomerOrders =scmCustomerOrdersService.selectScmCustomerOrdersById(scmOrderReceivables.getOrderId());
        if (scmCustomerOrders == null){
            throw new RuntimeException("订单编号非法");
        }
        ScmOrderReceivables scmOrderReceivablesOld = scmOrderReceivablesMapper.selectScmOrderReceivablesById(scmOrderReceivables.getId());
        if (scmOrderReceivablesOld == null){
            throw new RuntimeException("回款编号非法");
        }
        BigDecimal newTotalAmount = scmCustomerOrders.getReceivedAmount().subtract(scmOrderReceivablesOld.getMoney()).add(scmOrderReceivables.getMoney());
        scmCustomerOrders.setReceivedAmount(newTotalAmount);
        scmCustomerOrdersService.updateScmCustomerOrders(scmCustomerOrders);

        scmOrderReceivables.setUpdateTime(DateUtils.getNowDate());
        return scmOrderReceivablesMapper.updateScmOrderReceivables(scmOrderReceivables);
    }

    /**
     * 批量删除回款
     * 
     * @param ids 需要删除的回款主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteScmOrderReceivablesByIds(Long[] ids)
    {
        if (ids == null || ids.length == 0){
            throw new RuntimeException("请选择要删除的回款");
        }
        ScmOrderReceivables scmOrderReceivables = scmOrderReceivablesMapper.selectScmOrderReceivablesById(ids[0]);
        ScmCustomerOrders scmCustomerOrders =scmCustomerOrdersService.selectScmCustomerOrdersById(scmOrderReceivables.getOrderId());
        if (scmCustomerOrders == null){
            throw new RuntimeException("订单编号非法");
        }
        BigDecimal newTotalAmount = scmCustomerOrders.getReceivedAmount().subtract(scmOrderReceivables.getMoney());
        scmCustomerOrders.setReceivedAmount(newTotalAmount);
        scmCustomerOrdersService.updateScmCustomerOrders(scmCustomerOrders);

        return scmOrderReceivablesMapper.deleteScmOrderReceivablesByIds(ids);
    }

    /**
     * 删除回款信息
     * 
     * @param id 回款主键
     * @return 结果
     */
    @Override
    public int deleteScmOrderReceivablesById(Long id)
    {
        return scmOrderReceivablesMapper.deleteScmOrderReceivablesById(id);
    }
}
